
 /*===========================================
 *  Assignment: cpe301assignment1.asm
 *  
 *  By: Brian Clarke
 *  On: 2/18/2014 9:52:00 AM
 *============================================
 */ 
 ;Features:
 ;	X and Y pointers utilized
 ;	16 Bit checker with total sum storage
 ;	Total sum stored in R3:R4 
 ;	Division by 3 checker with total sum storage
 ;	All numbers divisible by 3 summed and stored in R20:R21
 ;	Division by 5 checker with total sum storage
 ;	All numbers divisible by 5 summed and stored in R23:R24

  .org 0
  
  LDI R26, HIGH(RAMEND) 	;Stack Initialization (High End)
  OUT SPH, R26
  LDI R26, LOW(RAMEND)		;Stack Initialization (Low End)
  OUT SPL, R26
  
  LDI R18, 20        		;Total number Counter
  LDI R17, 121      		;Stored value for number generation (>30 but <250)
  
  LoadStack:
   PUSH R17          		;Push the stored value onto the stack 
   DEC R17           		;Decrease R17 to get the next value
   DEC R18           		;Decrease counter 
   BRNE LoadStack    		;While R18 (the counter) is not 0, continue loading and decrementing

  LDI ZH, HIGH(RAMEND) 		;This will set Z to point to Stack (High)
  LDI ZL, LOW(RAMEND)		;Z pointer to Stack (Low)
  LDI YH, HIGH(RAMEND) 		;This will set Y to point to Stack
  LDI YL, LOW(RAMEND)		;Y pointer to Stack (Low)
  LDI XH, HIGH(RAMEND) 		;This will set X to point to Stack
  LDI XL, LOW(RAMEND)		;X pointer to Stack (Low)
  
  LDI R20, 0           		;R20 for the first portion of divisible by 3 sum
  LDI R21, 0				;R21 for the second portion
  LDI R22, 0           		;R22 is used for getting the quotient
  MOV R10, R22
  LDI R23, 0           		;R23 for the first portion of divisible by 5 sum
  LDI R24, 0		   		;R24 for the second portion
  LDI R18, 20		   		;Counter to sum the 20 values
  MOV R15, R18				;Counter for the other loops
  MOV R14, R18				;Counter for the other loops
  LDI R16, 255         		;Verification for a sum under 16 bits
  LDI R25, 3				;Initializes for dividing by 3
  
  ;TotalSum 
  ;  Sums and verifies the sum is under 16 bits


  TotalSum:        
  LD R1, Y           		;Load Y into R15
  ADD R3, R1         		;R3 (Processor view) contains the lower bits for the Total Sum.
  ADC R4, R2          		;R4 (Processor view) contains the higher bits for the Total Sum.          
  DEC YL             		;Decrease the pointer for next memory location
  DEC R18					;Decrement the counter
  BRNE TotalSum		 		;Keep summing until the counter is zero
  CP R4,R16					;Compare for bit checking
  BRLO SixteenBitsorLess	 ;Branch to check divisibility if sum is under 16 bits. 
  
  SixteenBitsorLess:
  LD R5, X			 		;Load X into R5
  MOV R19, R5        		;Move value from R5 into R19. Containing temp sum for divisible by 3
  DEC XL             		;Decrease the pointer for next memory location
  DEC R15
  BREQ GoToFive      		;Check if divisible by 5
  CLR R22            		;CLR R22, will be used again 
  rJMP ThreeDivCheck 		;Check if divisible by 3

  ThreeDivCheck:   			;If divisible by three, feeds into ThreeDiv (dividing by 3)
  INC R22			 		;Increment Quotient	
  SUB R5, R25		 		;Subtract 3 from R5 until 0 or negative
  BRCC ThreeDivCheck
  ADD R5, R25        		;If R5 = 0, then the number is divisible by 3
  BREQ ThreeDiv
  DEC R22			 		;Decrement to get the quotient
  BRNE SixteenBitsorLess    ;Branch to get the next value
  
  ThreeDiv:
  ADD R20, R19				;R19 = number divisible by 3, adds the number into the lower portion of the running sum (R20)
  ADC R21, R23      		;R21 contains the higher bit if needed
  rJMP SixteenBitsorLess

  GoToFive:
  CLR R19					;Clear R19
  LDI R19, 5				;Reinitializes with 5 for division
  LD R6, Z					;Load Z into R6
  MOV R11, R6				;Move R6 into R11. This is the running sum for numbers divisible by 5
  DEC ZL					;Decrease the pointer for next memory location
  DEC R14					;Decrement Counter
  BREQ Done			;Branch if done
  CLR R22					;Clear R22
  rJMP FiveDivCheck			;Check if divisible by 5

  FiveDivCheck:   			;If divisible by five, feeds into FiveDiv (dividing by 5)
  INC R10					;Increment Quotient
  SUB R6, R19				;Subtract 5 from the number until 0 or negative
  BRCC FiveDivCheck
  ADD R6, R19				;If R6 = 0, then the number is divisible by 5
  BREQ FiveDiv	
  DEC R10					;Decrement to get our quotient
  BRNE GoToFive				;Branch to get the next number 

  FiveDiv:
  ADD R23, R11				;R11 = number divisible by 5, adds the number into the lower portion of the running sum (R23)
  ADC R24, R12				;R24 contains the higher bit if needed
  rJMP GoToFive


Done:
JMP Done				;Done
  